package test;

import java.util.concurrent.TimeUnit;

public class Task extends Thread {

    private TaskEngine taskEngine;
    //
    private volatile String id;
    private volatile boolean wait = false;
    private volatile boolean stop = false;

    public Task(TaskEngine taskEngine, String id) {
        this.taskEngine = taskEngine;
        this.id = id;
    }

    public TaskEngine getTaskEngine() {
        return taskEngine;
    }

    public void setTaskEngine(TaskEngine taskEngine) {
        this.taskEngine = taskEngine;
    }

    public boolean isWait() {
        return wait;
    }

    public void setWait(boolean wait) {
        if (this.stop) {
            return;
        }
        this.wait = wait;
    }

    public boolean isStop() {
        return stop;
    }

    public void setStop(boolean stop) {
        if (!stop) {
            throw new IllegalStateException("not false");
        }
        this.stop = true;
    }
    //

    public void check() throws TaskEnginStopException, TaskStopException, InterruptedException {
        System.out.println("wait:" + isWait() + ",stop:" + isStop() + ",taskengine wait:" + taskEngine.isWait() + ",task engine stop:" + taskEngine.isStop());
        //stop是终止状态,Task比TaskEngin的执行的优先级快
        if (this.isStop()) {
            throw new TaskStopException();
        }
        if (taskEngine.isStop()) {
            throw new TaskEnginStopException();
        }
        //
        //wait不是是终止状态，可以进行恢复,因为Task比TaskEngin的执行的优先级快,所以this.wait在taskEngine.isWait()前面
        while (this.isWait() || taskEngine.isWait()) {
            TimeUnit.SECONDS.sleep(1);
            //
            if (this.isStop()) {
                throw new TaskStopException();
            }
            if (taskEngine.isStop()) {
                throw new TaskEnginStopException();
            }
        }
    }

    @Override
    public void run() {
        //super.run();
        try {
            doTask();
        } catch (TaskEnginStopException e) {
            e.printStackTrace();
        } catch (TaskStopException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("" + id);
            getTaskEngine().getTaskMapping().getTaskMap().remove(id);
        }
    }

    public void doTask() throws TaskEnginStopException, TaskStopException, InterruptedException {
        TimeUnit.SECONDS.sleep(10);
        System.out.println("check 1 start");
        check();
        System.out.println("check 1 end");

        //
        TimeUnit.SECONDS.sleep(10);
        System.out.println("check 1 start");
        check();
        System.out.println("check 1 end");
        //
        TimeUnit.SECONDS.sleep(10);
        System.out.println("check 1 start");
        check();
        System.out.println("check 1 end");

        //
        TimeUnit.SECONDS.sleep(10);
        System.out.println("check 1 start");
        check();
        System.out.println("check 1 end");
        //
        TimeUnit.SECONDS.sleep(10);
        System.out.println("check 1 start");
        check();
        System.out.println("check 1 end");
        //
        TimeUnit.SECONDS.sleep(10);
        System.out.println("check 1 start");
        check();
        System.out.println("check 1 end");

    }
}
